package com.surpassun.emall.controller;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.surpassun.emall.model.Store;
import com.surpassun.emall.service.RoleService;
import com.surpassun.emall.service.StoreService;
import com.surpassun.emall.service.UserService;
import com.surpassun.emall.util.CacheUtil;
import com.surpassun.emall.util.Constants;
import com.surpassun.emall.util.Validator;

@Controller
@RequestMapping("/admin/**")
public class AdminController {
	
	@Inject
	StoreService storeService;
	
	@Inject
	RoleService roleService;
	
	@Inject
	UserService userService;
	
	@RequestMapping("/admin")
	public String view(HttpServletRequest request, Model model) {
		return renderStore(request, model);
	}
	
	@RequestMapping("/admin/store")
	public String renderStore(HttpServletRequest request, Model model) {
		@SuppressWarnings("unchecked")
		List<Store> stores = (List<Store>)CacheUtil.getObject(Constants.ATTR_STORES);
		if (Validator.isNull(stores)) {
			stores = storeService.getAll();
		}
		model.addAttribute(Constants.ATTR_STORES, stores);
		return "adminStore";
	}
	
	@RequestMapping("/admin/user")
	public String renderUser(HttpServletRequest request, Model model) {
		return "adminUser";
	}
	
	@RequestMapping("/admin/role")
	public String renderRole(HttpServletRequest request, Model model) {
		return "adminRole";
	}
}
